// pages/my/myCarSource.js
var $api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: "", //城市
    tab: "onSale",//当前标签页
    s0: [], //车源列表
    s1: [], //车源列表
    s2: [], //车源列表
    s3: [], //车源列表
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      city: wx.getStorageSync('city')
    })
    this.getMyCarSource();
  },

  del: function (e) {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '确认删除该车源吗？',
      success (res) {
        if (res.confirm) {
          $api.sourceDel({goods_id: e.currentTarget.dataset.id}).then(res => {
            if (res.code == 1) {
              self.getMyCarSource();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  
  xiajia: function (e) {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '确认下架该车源吗？',
      success (res) {
        if (res.confirm) {
          $api.postCar({id: e.currentTarget.dataset.id, status: 3}).then(res => {
            if (res.code == 1) {
              self.getMyCarSource();
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  goEdit: function (e) {
    var listData = JSON.stringify(e.target.dataset.detail);
    wx.navigateTo({
        url: '/pages/service/sellCar?data=' + listData,
    })
  },

  //标签tab改变方法
  onChange: function (option) {
    this.setData({
      tab: option.detail.name
    })
  },
  
  openVehicleDetails: function (event) {
      let vehicleId = event.currentTarget.dataset.vehicleid;
      wx.navigateTo({
          url: '/pages/index/vehicleDetails?vehicleId=' + vehicleId,
      })
  },

  getMyCarSource: function () {
    let params;
    $api.myCarSource(params).then(res => {
      if (res.code == 1) {
        var data = res.data;
        var s0 = data.filter(item => item.status == 0);
        var s1 = data.filter(item => item.status == 1);
        var s2 = data.filter(item => item.status == 2);
        var s3 = data.filter(item => item.status == 3);
        this.setData({
          s0,
          s1,
          s2,
          s3
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})